home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / VideoFolder 1.0a / Source / VideoFolderDocument.cp < prev    next >
Text File  |  1996-06-21  |  7KB  |  246 lines

  1. #include "VideoFolderDocument.h"
  2.  
  3. #include "FSSpecPane.h"
  4. #include "SequenceGrabberPane.h"
  5. #include "VideoFolderWindow.h"
  6.  
  7. const ResIDT    window_Sample        = 1;    // EXAMPLE
  8.  
  9. VideoFolderDocument::VideoFolderDocument() :
  10.     mVideoFolderWindow ( nil ),
  11.     mFile ( nil )
  12. {
  13.     mVideoFolderWindow = dynamic_cast<VideoFolderWindow*> ( LWindow::CreateWindow(window_Sample, this) );
  14.     Assert_ ( mVideoFolderWindow );
  15.             
  16.     mVideoFolderWindow->Show();
  17. }
  18.  
  19. VideoFolderDocument::VideoFolderDocument ( LCommander* inSuper, const FSSpec* inSpec ) :
  20.     mVideoFolderWindow ( nil ),
  21.      mFile ( nil )
  22. {
  23.     mVideoFolderWindow = dynamic_cast<VideoFolderWindow*> ( LWindow::CreateWindow(window_Sample, this) );
  24.     Assert_ ( mVideoFolderWindow );
  25.             
  26.     if ( inSpec )
  27.         OpenFile ( *inSpec );
  28.     else
  29.         NameNewDoc();
  30.         
  31.     mVideoFolderWindow->Show();
  32. }
  33.  
  34. VideoFolderDocument::~VideoFolderDocument ( )
  35. {
  36. }
  37.  
  38. Boolean
  39. VideoFolderDocument::AllowSubRemoval(
  40.     LCommander    *inSub)
  41. {
  42.     if (inSub == mVideoFolderWindow) {
  43.     
  44.             // Check if the current AppleEvent is a "close" event
  45.             // sent to the Window. If so, we handle it as if the
  46.             // "close" event were sent to the Document
  47.              
  48.         AppleEvent    currentEvent;
  49.         DescType    theType;
  50.         DescType    theAttr = typeNull;
  51.         Size        theSize;
  52.         ::AEGetTheCurrentEvent(¤tEvent);
  53.         if (currentEvent.descriptorType != typeNull) {
  54.             ::AEGetAttributePtr(¤tEvent, keyEventClassAttr,
  55.                 typeType, &theType, &theAttr, sizeof(DescType),
  56.                 &theSize);
  57.             if (theAttr == kAECoreSuite) {
  58.                 ::AEGetAttributePtr(¤tEvent, keyEventIDAttr,
  59.                     typeType, &theType, &theAttr, sizeof(DescType),
  60.                     &theSize);
  61.                 if (theAttr == kAEClose) {
  62.                     DoAEClose(currentEvent);
  63.                     return false;
  64.                 }
  65.             }
  66.         }
  67.         
  68.         AttemptClose(true);            // A non-AppleEvent close
  69.         return false;
  70.  
  71.     } else {
  72.         return true;
  73.     }
  74. }
  75.  
  76. // ---------------------------------------------------------------------------
  77. //        • GetDescriptor
  78. // ---------------------------------------------------------------------------
  79. //    Pass back the name of a Document
  80.  
  81. StringPtr
  82. VideoFolderDocument::GetDescriptor(
  83.     Str255    outDescriptor) const
  84. {
  85.     if ((mFile != nil) && mIsSpecified) {
  86.         FSSpec    fileSpec;            // Document name is same as its File
  87.         mFile->GetSpecifier(fileSpec);
  88.         LString::CopyPStr(fileSpec.name, outDescriptor);
  89.     
  90.     } else if (mVideoFolderWindow != nil) {    // No File, use name of its Window
  91.         mVideoFolderWindow->GetDescriptor(outDescriptor);
  92.     
  93.     } else {                        // No File and No Window
  94.         outDescriptor[0] = 0;        //   Document name is empty string
  95.     }
  96.     
  97.     return outDescriptor;
  98. }
  99.  
  100. void
  101. VideoFolderDocument::NameNewDoc()
  102. {
  103.     // Setup the window title. Start with the default title.
  104.     LStr255    theTitle( "\pUntitled" );
  105.  
  106.     // Find the first available title. We could also check the window
  107.     // pane id if we wanted to make sure we didn't collide with other
  108.     // window types.
  109.     Int32    theNumber = 0;
  110.     while ( UWindows::FindNamedWindow( theTitle ) != nil ) {
  111.  
  112.         // An existing window has the current name
  113.         // Increment counter and try again.
  114.         ++theNumber;
  115.         theTitle = "\pUntitled ";
  116.         theTitle += (LStr255) theNumber;
  117.  
  118.     }        
  119.     
  120.     // Finally, set window title.
  121.     mVideoFolderWindow->SetDescriptor( theTitle );
  122. }
  123.  
  124. void VideoFolderDocument::OpenFile ( const FSSpec& inFileSpec )
  125. {
  126.     Try_ {
  127.  
  128.         // Create a new file object.
  129.         mFile = new LFileStream ( inFileSpec );
  130.         
  131.         // Open the data fork.
  132.         mFile->OpenDataFork( fsRdWrPerm );
  133.                 
  134.         // Set window title to the name of the file.
  135.         mVideoFolderWindow->SetDescriptor( inFileSpec.name );
  136.  
  137.         unsigned long signature;
  138.         mFile->ReadData ( & signature, sizeof( signature ) );
  139.         ThrowIfNot_ ( signature == 'KSS!' );
  140.         
  141.         unsigned long version;
  142.         mFile->WriteData ( & version, sizeof( version ) );
  143.         ThrowIfNot_ ( version == 1 );
  144.         
  145.         unsigned long interval = mVideoFolderWindow->GetUpdateInterval();
  146.         mFile->ReadData ( & interval, sizeof ( interval ) );
  147.         mVideoFolderWindow->SetUpdateInterval ( interval );
  148.  
  149.         unsigned long liveUpdate = true;
  150.         LPane* liveUpdatePane = dynamic_cast<LPane*> ( mVideoFolderWindow->FindPaneByID ( 'LIVE' ) );
  151.         mFile->ReadData ( & liveUpdate, sizeof( liveUpdate ) );
  152.         if ( liveUpdatePane )
  153.             liveUpdatePane->SetValue( liveUpdate );
  154.         
  155.         FSSpec spec = { 0, 0, "\p" };
  156.         FSSpecPane* destFSSpecPane = dynamic_cast<FSSpecPane*> ( mVideoFolderWindow->FindPaneByID ( 'DSTF' ) );
  157.         mFile->ReadData ( & spec, sizeof( spec ) );
  158.         if ( destFSSpecPane )
  159.             destFSSpecPane->SetFSSpec ( spec );                
  160.             
  161.         SequenceGrabberPane* grabberPane = dynamic_cast<SequenceGrabberPane*> ( mVideoFolderWindow->FindPaneByID ( 'sgrb' ) );
  162.         ThrowIfNil_( grabberPane );
  163.         Handle configurationHandle = nil;
  164.         mFile->ReadHandle ( configurationHandle );
  165.         grabberPane->SetConfiguration ( configurationHandle );
  166.         DisposeHandle ( configurationHandle );
  167.  
  168.         mFile->CloseDataFork();
  169.  
  170.         // Flag that document has an associated file.
  171.         mIsSpecified = true;
  172.  
  173.     } Catch_( inErr ) {
  174.  
  175.         // Cleanup and rethrow the error.
  176.         delete this;
  177.         Throw_( inErr );
  178.     
  179.     } EndCatch_
  180. }
  181.  
  182. void VideoFolderDocument::DoAESave(FSSpec &inFileSpec, OSType inFileType)
  183. {
  184.     // Delete the existing file object.
  185.     delete mFile;
  186.     
  187.     // Make a new file object.
  188.     mFile = new LFileStream( inFileSpec );
  189.     
  190.     // Get the proper file type.
  191.     OSType    theFileType = 'VFol';
  192.     if ( inFileType != fileType_Default ) theFileType = inFileType;
  193.  
  194.     // Make new file on disk (we'll use
  195.     // SimpleText's creator for this example).
  196.     mFile->CreateNewDataFile( 'ttxt', theFileType );
  197.     
  198.     // Write out the data.
  199.     DoSave();
  200.  
  201.     // Change window title to reflect the new name.
  202.     mVideoFolderWindow->SetDescriptor( inFileSpec.name );
  203.  
  204.     // Document now has a specified file.
  205.     mIsSpecified = true;
  206. }
  207.  
  208. void VideoFolderDocument::DoSave ( )
  209. {
  210.     ThrowIfNil_( mVideoFolderWindow );
  211.     
  212.     // Open the data fork.
  213.     mFile->OpenDataFork( fsRdWrPerm );
  214.  
  215.     unsigned long signature = 'KSS!';
  216.     mFile->WriteData ( & signature, sizeof( signature ) );
  217.     
  218.     unsigned long version = 1;
  219.     mFile->WriteData ( & version, sizeof( version ) );
  220.  
  221.     
  222.     unsigned long interval = mVideoFolderWindow->GetUpdateInterval();
  223.     mFile->WriteData ( & interval, sizeof ( interval ) );
  224.  
  225.     unsigned long liveUpdate = true;
  226.     LPane* liveUpdatePane = dynamic_cast<LPane*> ( mVideoFolderWindow->FindPaneByID ( 'LIVE' ) );
  227.     if ( liveUpdatePane )
  228.         liveUpdate = liveUpdatePane->GetValue() != 0;
  229.     mFile->WriteData ( & liveUpdate, sizeof( liveUpdate ) );
  230.     
  231.     FSSpec spec = { 0, 0, "\p" };
  232.     FSSpecPane* destFSSpecPane = dynamic_cast<FSSpecPane*> ( mVideoFolderWindow->FindPaneByID ( 'DSTF' ) );
  233.     if ( destFSSpecPane )
  234.         destFSSpecPane->GetFSSpec ( spec );
  235.             
  236.     mFile->WriteData ( & spec, sizeof( spec ) );
  237.         
  238.     SequenceGrabberPane* grabberPane = dynamic_cast<SequenceGrabberPane*> ( mVideoFolderWindow->FindPaneByID ( 'sgrb' ) );
  239.     ThrowIfNil_( grabberPane );
  240.     Handle configurationHandle = grabberPane->GetConfiguration ( );
  241.     mFile->WriteHandle ( configurationHandle );
  242.     DisposeHandle ( configurationHandle );
  243.     
  244.     // Close the data fork.
  245.     mFile->CloseDataFork();
  246. }